<?php

class Advertiser extends Model_Abstract
{
	
	public function getAllAdvertisers() {
		$sql = "select * from advertisers";
		try {
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAdvertiser($id) {
		$sql = "select * from advertisers where id=?";
		try {
			$result = $this->_connection->query($sql, array($id));
			if (count($result) > 0)
				return $result[0];
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function addNewAdvertisers($advertisers) {
		$sql = "insert into advertisers (name) values" . str_repeat(' (?), ', count($advertisers));
		try {
			$result = $this->_connection->execute(substr($sql, 0, -2), $advertisers);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function updateAdvertiser($id, $name)
	{
		$sql = "update advertisers set name=? where id=? ";
		try {
			$result = $this->_connection->execute($sql, array($name, $id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function deleteAdvertiser($id)
	{
		$sql = "delete from advertisers where id=?";
		try {
			$result = $this->_connection->execute($sql, array($id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function addAdvertiserData($advetiser, $site, $click, $revenue)
	{
		
	}
	
}